package com.niiwoo.civet.user.service.duboo;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.user.dto.response.ThirdAuthResponseDTO;
import com.niiwoo.civet.user.enums.UserDataTypeEnum;
import com.niiwoo.civet.user.service.UserAuthCreditCardDubboService;
import com.niiwoo.civet.user.service.local.*;
import com.niiwoo.tripod.tiancheng.component.TianchengAuthCreditCardService;
import com.niiwoo.tripod.tiancheng.component.TianchengAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @author zzl
 * @create 2018-03-06 16:31
 * @desc 用户信用卡授权实现类
 **/
@Slf4j
@Service(version = "1.0.0")
public class UserAuthCreditCardDubboServiceImpl extends UserAuthCommonTemplateService implements UserAuthCreditCardDubboService {

    @Autowired
    UserCreditCardAuthLocalService userCreditCardAuthLocalService;

    @Autowired
    TianchengAuthCreditCardService tianchengAuthCreditCardService;

    /**
     * 上次授权失败原因显示结果页面
     */
    private final String h5ErrorMsgUrl = "{0}/html5/project/creditresult/index.html#/authorizeFail?t={1}&msg={2}";

    /**
     * 天秤授权同步get回调h5页面地址
     */
    private final String h5CallbackResultUrl = "/html5/project/creditresult/index.html#/";

    /**
     * 天秤授权数据回调url
     */
    private final String dataCallbackUrl="/tc/authCreditCardData/callback";

    @Override
    public ThirdAuthResponseDTO getThirdAuthUrl(String userId, String createIp, String source) {
        return super.getThirdAuthUrl(userId, createIp, source);
    }

    @Override
    public boolean authCallback(String resultJson) {
        return super.authCallback(resultJson);
    }

    @Override
    public boolean dataCallBack(String resultJson) {
        return super.dataCallBack(resultJson);
    }


    @Override
    public String getH5ErrorMsgUrl() {
        return h5ErrorMsgUrl;
    }

    @Override
    public String getH5CallbackResultUrl() {
        return h5CallbackResultUrl;
    }

    @Override
    public String getDataCallbackUrl() {
        return dataCallbackUrl;
    }

    @Override
    public String getAuthBizName() {
        return "CreditCardAuth";
    }

    @Override
    public String getAuthChineseName() {
        return "信用卡";
    }

    @Override
    public UserDataTypeEnum getDataType() {
        return UserDataTypeEnum.CREDIT_CARD_AUTHORIZE;
    }

    @Override
    public void saveDataCallback(String userId, String userInfoString) {
        //保存数据回调时的状态数据
        String authName = null;
        String entryId = null;
        JSONObject userInfo = JSONObject.parseObject(userInfoString);
        if (userInfo != null) {
            authName = userInfo.getString("yxLoginName");
            entryId = userInfo.getString("yxEntryId");
        }
        userCreditCardAuthLocalService.saveUserCreditCardAuthInfo(userId, authName, entryId, new Date());
    }

    @Override
    public TianchengAuthService getTianchengAuthService() {
        return tianchengAuthCreditCardService;
    }
}
